// Copyright 2011, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.ads.common.lib.conf;

import com.google.api.ads.common.lib.utils.logging.AdsServiceLoggers;
import com.google.common.base.Throwables;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

import java.io.File;
import java.net.URL;
import java.security.AccessControlException;

/**
 * Helper class that loads {@link Configuration} from various sources.
 *
 * @author Kevin Winter
 * @author Jeff Sham
 */
public class ConfigurationHelper {

  /**
   * Loads configuration from a specified path. If not absolute, will look in user home directory,
   * the current classpath and the system classpath.  Absolute classpath references will not work.
   *
   * @param path the path to try first as a resource, then as a file
   * @returns properties loaded from the specified path or null.
   */
  public Configuration fromFile(String path) {
    PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
    try {
      propertiesConfiguration.load(path);
    } catch (ConfigurationException e) {
      if (Throwables.getRootCause(e) instanceof AccessControlException){
        AdsServiceLoggers.ADS_API_LIB_LOG.debug("Module could not be loaded.", e);
      } else {
        throw new IllegalArgumentException(
            "Encountered a problem reading the provided configuration file \"" + path + "\"!", e);
      }
    }
    return propertiesConfiguration;
  }

  /**
   * Loads configuration from a specified path.
   *
   * @param path the path to try first as a resource, then as a file
   * @returns properties loaded from the specified path or null.
   */
  public Configuration fromFile(File path) {
    try {
      return new PropertiesConfiguration(path);
    } catch (ConfigurationException e) {
      throw new IllegalArgumentException(
          "Encountered a problem reading the provided configuration file \"" + path + "\"!", e);
    }
  }

  /**
   * Loads configuration from a specified path.
   *
   * @param path the path to try first as a resource, then as a file
   * @returns properties loaded from the specified path or null.
   */
  public Configuration fromFile(URL path) {
    try {
      return new PropertiesConfiguration(path);
    } catch (ConfigurationException e) {
      throw new IllegalArgumentException(
          "Encountered a problem reading the provided configuration file \"" + path + "\"!", e);
    }
  }
}
